package com.cqjtu.store.cart.service.impl;

import com.cqjtu.store.cart.mapper.CartMapper;
import com.cqjtu.store.cart.pojo.dto.CartDTO;
import com.cqjtu.store.cart.pojo.dto.entity.ShoppingCart;
import com.cqjtu.store.cart.service.CartService;
import com.cqjtu.store.common.ServiceCode;
import com.cqjtu.store.common.ServiceException;
import com.cqjtu.store.product.mapper.ProductMapper;
import com.cqjtu.store.product.pojo.entity.Product;
import com.cqjtu.store.product.pojo.vo.ShoppingCartVO;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询用户的购物车商品列表
     * @param userLoginVO   用户信息
     * @return
     */
    @Override
    public List<ShoppingCartVO> getShoppingCartList(UserLoginVO userLoginVO) {
        //根据用户id获取用户的购物车信息
        List<ShoppingCart> shoppingCartList = cartMapper.getShoppingCartList(userLoginVO.getId());
        if(shoppingCartList.isEmpty()){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该用户的购物车中没有商品");
        }
        List<ShoppingCartVO> shoppingCartVOList = new ArrayList<>();
        //封装
        shoppingCartList.forEach(shoppingCart -> {
            ShoppingCartVO shoppingCartVO = new ShoppingCartVO();
            shoppingCartVO.setId(shoppingCart.getId());
            shoppingCartVO.setUserId(shoppingCart.getId());
            shoppingCartVO.setProductId(shoppingCart.getProductId());
            shoppingCartVO.setPrice(shoppingCart.getPrice());
            shoppingCartVO.setNum(shoppingCart.getNum());
            shoppingCartVO.setCreatedUser(shoppingCart.getCreatedUser());
            shoppingCartVO.setCreatedTime(shoppingCart.getCreatedTime());
            shoppingCartVO.setModifiedUser(shoppingCart.getModifiedUser());
            shoppingCartVO.setModifiedTime(shoppingCart.getModifiedTime());
            //获取根据商品的id查询商品标题和图片路径
            Product product = productMapper.getProductById(shoppingCart.getProductId());
            shoppingCartVO.setTitle(product.getTitle());
            shoppingCartVO.setImage(product.getImage());
            shoppingCartVOList.add(shoppingCartVO);

        });
        return shoppingCartVOList;
    }

    /**
     * 删除购物车中的商品
     * @param carts 待删除的商品
     * @param userLoginVO   用户信息
     */
    @Override
    public void deleteCarts(CartDTO[] carts, UserLoginVO userLoginVO) {
        for (int i = 0; i < carts.length; i++) {
            cartMapper.delete(carts[i].getId(),userLoginVO.getId());
        }
    }

    @Override
    public void updateNum(CartDTO cartDTO, UserLoginVO userLoginVO) {
        List<ShoppingCart> shoppingCartList = cartMapper.getShoppingCartList(userLoginVO.getId());
        if(shoppingCartList.isEmpty()){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该用户的购物车中没有商品");
        }
        cartMapper.updateNum(cartDTO.getNum(),cartDTO.getId(),userLoginVO.getId());
    }

    /**
     * 将用户购物车中某商品的数量加一
     * @param productId
     * @param userLoginVO
     */
    @Override
    public void addNum(Integer productId, UserLoginVO userLoginVO) {
        int result = cartMapper.addNum(productId, userLoginVO.getId());
        if (result != 1){
            throw new ServiceException(ServiceCode.ERR_UPDATE_FAILED,"数量加一失败");
        }
    }

    /**
     *将用户购物车中某商品的数量减一
     * @param productId
     * @param userLoginVO
     */
    @Override
    public void reduceNum(Integer productId, UserLoginVO userLoginVO) {
        int result = cartMapper.reduceNum(productId, userLoginVO.getId());
        if (result != 1){
            throw new ServiceException(ServiceCode.ERR_UPDATE_FAILED,"数量加一失败");
        }
    }

    /**
     * 加购商品
     * @param cartDTO
     * @param userLoginVO
     */
    @Override
    public void purchase(CartDTO cartDTO, UserLoginVO userLoginVO){
        ShoppingCart shoppingCart=new ShoppingCart();
        //查询该商品价格&赋值
        int price = cartMapper.getPrice(cartDTO.getId());
        shoppingCart.setPrice(price);
        //封装shoppingCart
        shoppingCart.setUserId(userLoginVO.getId());
        shoppingCart.setProductId(cartDTO.getId());
        shoppingCart.setNum(cartDTO.getNum());
        shoppingCart.setCreatedUser(userLoginVO.getUsername());
        shoppingCart.setModifiedUser(userLoginVO.getUsername());
        Date date=new Date();
        shoppingCart.setCreatedTime(date);
        shoppingCart.setModifiedTime(date);
        //写入数据库
        int result = cartMapper.purchase(shoppingCart);
        if(result != 1){
            throw  new ServiceException(ServiceCode.ERR_UPDATE_FAILED,"加购失败");
        }
    }
}
